<?php
class News extends CI_Controller{
	public function index()
	{
		
	}
	
    function item()
    {
        $id = $this->input->get('id');
        if(!empty($id))
        {
            $this->load->library('ckeditor');
            $data['ckeditor'] = $this->ckeditor->header;
            $this->load->model('news_model','news');
            $data['item'] = $this->news->get_item($id);
            $data['category'] = $this->news->get_list_category();
            $this->load->view('news/item',$data);
            
        }else{
            
            $this->load->library('ckeditor');
            $data['ckeditor'] = $this->ckeditor->header;
            $this->load->model('news_model','news');
            $data['category'] = $this->news->get_list_category();
            $this->load->view('news/item',$data);
        }
    }
    
	function postpage()
    {
        
        $this->load->library('ckeditor');
        $data['ckeditor'] = $this->ckeditor->header;
        $this->load->model('news_model');
        $data['category'] = $this->news_model->get_list_category();
        $this->load->view('news/postpage',$data);
    }
    
    function postnews()
    {
        $data = $this->input->post();
        $this->load->model('news_model');
        $this->news_model->postnews($data);
    }
    
    function getdomain($url)
    {
        return str_ireplace('www.','',parse_url($url,PHP_URL_HOST));
    }
    
    function geturl()
    {
        $this->load->library('curl');
        $url = $this->input->get('url');
        if(empty($url))
            $url = 'http://www.vnexpress.net';
        //$content = file_get_contents($url);
        $content = $this->curl->get($url);
        $content = $this->preg_rep($content,$url);
        $output = preg_split("/(<body.*?>)/i",$content,0,PREG_SPLIT_DELIM_CAPTURE);
        echo $output[0].$output[1];
        $this->load->library('ckeditor');
        $data['ckeditor'] = $this->ckeditor->header;
        $this->load->model('news_model','news');
        $data['category'] = $this->news->get_list_category();
        $data['source'] = $this->getdomain($url);
        $this->load->view('news/item',$data);
        echo $output[2];
        /*$this->load->library('session');
        if(!$this->session->userdata('urlarray'))
        {
            $this->session->set_userdata('urlarray',array());
            $this->session->set_userdata('urlactive',null);
        }
        
        if($url == 1 || $url == -1)
        {
            $urlarray = $this->session->userdata('urlarray');
            $urlactive = $this->session->userdata('urlactive');
            foreach($urlarray as $k => $v)
            {
                if($v == $urlactive)
                    $item = $k + $url;break;
            }
            
            $this->session->set_userdata('urlactive', $urlarray[$item]);
            $content = file_get_contents($urlarray[$item]);
            echo $this->preg_rep($content,$urlarray[$item]);
        }else{
            
            $urlarray = $this->session->userdata('urlarray');
            $urlarray[] = $url;
            $this->session->set_userdata('urlarray',$urlarray);
            $this->session->set_userdata('urlative',$url);
            $content = file_get_contents($url);
            echo $this->preg_rep($content,$url);
        }*/
        
        
    }
    
    function preg_rep($content,$url)
    {
        $domain = 'http://'.parse_url($url, PHP_URL_HOST);
        $pattern[0] = '/([^<a.*?])href="([^http].*?)"/i';
        $rep[0] = '${1}href="'.$domain.'${2}"';
        $pattern[1] = '/([^<a.*?])src="([^http].*?)"/i';
        $rep[1] = '${1}src="'.$domain.'${2}"';
        $pattern[2] = '/([^<a.*?])href=\'([^http].*?)\'/i';
        $rep[2] = '${1}href="'.$domain.'${2}"';
        $pattern[3] = '/([^<a.*?])src=\'([^http].*?)\'/i';
        $rep[3] = '${1}src="'.$domain.'${2}"';
        $content = preg_replace($pattern,$rep,$content);
        $pattern1 = '/(<a.*?)href="([^http].*?)"/i';
        $rep1 = '${1}href="'.$domain.'${2}"';
        $content = preg_replace($pattern1,$rep1,$content);
        $pattern2 = '/(<a.*?)href="([http].*?)"/i';
        $rep2 = '${1} href="http://localhost/notenews/news/geturl?url=${2}"';
        //$rep2 = '${1} onclick="do_get(\'${2}\')"';
        $content = preg_replace($pattern2,$rep2,$content);
        return $content;
    }
}
?>
